﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.Data;
using V5_EShop.Data;

namespace V5_EShop.Admin.Book {
    public partial class Admin_Book : AdminPage {

        #region Tbl_B_Book开始
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string action = InputHelper.SetString(Request["action"]);
                this.Bind_DropDownList();
                switch (action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_DropDownList() {
            this.ddlClassId.DataSource = V5.Class.GetDropDownList("Tbl_B_BookClass", 0, "请选择");
            this.ddlClassId.DataTextField = "ClassTitle";
            this.ddlClassId.DataValueField = "ClassId";
            this.ddlClassId.DataBind();

            DataTable dt = V5.SQL.Query_By("*", "Tbl_B_BookPublish", "", "Id Desc", 0);
            this.ddlPubId.DataSource = dt;
            this.ddlPubId.DataTextField = "PubName";
            this.ddlPubId.DataValueField = "Id";
            this.ddlPubId.DataBind();
            this.ddlPubId.Items.Insert(0, new ListItem("请选择", "0"));

            dt = V5.SQL.Query_By("*", "Tbl_B_BookIsbn", "", "Id Desc", 0);
            this.ddlIsbnKind.DataSource = dt;
            this.ddlIsbnKind.DataTextField = "IsbnKind";
            this.ddlIsbnKind.DataValueField = "Id";
            this.ddlIsbnKind.DataBind();
            this.ddlIsbnKind.Items.Insert(0, new ListItem("请选择", "0"));
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = V5.SQL.Query_Pager("B.ClassTitle,A.*", "Tbl_B_Book A Left Join Tbl_B_BookClass B On A.ClassId=B.ClassId", "", "A.Id Desc", Pager.PageSize, Pager.CurrentPageIndex, ref RecordCount);
            Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        protected void Bind_DataDelete(int id) {
            this.Bind_DataList();
        }

        protected void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            DataRow dr = V5.SQL.Query_By("*", "Tbl_B_Book", "Id=" + id, string.Empty, 1).Rows[0];
            this.hfID.Value = dr["Id"].ToString();
            this.txtIsbn.Text = dr["Isbn"].ToString();
            this.txtBookName.Text = dr["BookName"].ToString();
            this.ddlPubId.SelectedIndex = this.ddlPubId.Items.IndexOf(
                this.ddlPubId.Items.FindByValue(dr["PubId"].ToString())
                );
            this.txtPrice.Text = dr["Price"].ToString();
            this.txtSellPrice.Text = dr["SellPrice"].ToString();
            this.txtPubDate.Text = dr["PubDate"].ToString();
            this.chkIfhot.Checked = dr["Ifhot"].ToString() == "1" ? true : false;
            this.txtRecDate.Text = dr["RecDate"].ToString();
            this.txtJhzk.Text = dr["Jhzk"].ToString();
            this.txtPxzk.Text = dr["Pxzk"].ToString();
            this.txtLszk.Text = dr["Lszk"].ToString();
            this.txtAuthor.Text = dr["Author"].ToString();
            this.txtZiShu.Text = dr["ZiShu"].ToString();
            this.txtZhzhen.Text = dr["Zhzhen"].ToString();
            this.txtKaiBen.Text = dr["KaiBen"].ToString();
            this.txtKcMax.Text = dr["KcMax"].ToString();
            this.txtKcMin.Text = dr["KcMin"].ToString();
            this.txtYinCi.Text = dr["YinCi"].ToString();
            this.ddlIsbnKind.SelectedIndex = this.ddlIsbnKind.Items.IndexOf(
                this.ddlIsbnKind.Items.FindByValue(dr["IsbnKind"].ToString())
                ); ;
            this.txtWeiZhi.Text = dr["WeiZhi"].ToString();
            this.txtYinZhang.Text = dr["YinZhang"].ToString();
            this.txtNote.Text = dr["Note"].ToString();
            this.lblTitle.Text = "修改图书";
            this.btnSave.Text = "确 定 保 存";
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int Id = InputHelper.SetNumber(this.hfID.Value);
            int BookId = 0;
            string Isbn = this.txtIsbn.Text;
            string BookName = this.txtBookName.Text;
            int PubId = InputHelper.SetNumber(this.ddlPubId.SelectedValue);
            decimal Price = decimal.Parse(this.txtPrice.Text);
            decimal SellPrice = decimal.Parse(this.txtSellPrice.Text);
            DateTime PubDate = DateTime.Parse(this.txtPubDate.Text);
            int Ifhot = this.chkIfhot.Checked ? 1 : 0;
            DateTime RecDate = DateTime.Parse(this.txtRecDate.Text);
            string Jhzk = this.txtJhzk.Text;
            string Pxzk = this.txtPxzk.Text;
            string Lszk = this.txtLszk.Text;
            string Author = this.txtAuthor.Text;
            int ZiShu = InputHelper.SetNumber(this.txtZiShu.Text);
            string Zhzhen = this.txtZhzhen.Text;
            string KaiBen = this.txtKaiBen.Text;
            int KcMax = InputHelper.SetNumber(this.txtKcMax.Text);
            int KcMin = InputHelper.SetNumber(this.txtKcMin.Text);
            int YinCi = InputHelper.SetNumber(this.txtYinCi.Text);
            int IsbnKind = InputHelper.SetNumber(this.ddlIsbnKind.Text);
            string WeiZhi = this.txtWeiZhi.Text;
            string YinZhang = this.txtYinZhang.Text;
            string Note = this.txtNote.Text;
            DateTime CreateTime = DateTime.Now;
            DateTime UpdateTime = DateTime.Now;

            ModelB_Book model = new ModelB_Book();
            if (Id > 0) {
                model = new ModelB_Book(p => p.Id == Id);
            }
            model.BookId = BookId;
            model.Isbn = Isbn;
            model.BookName = BookName;
            model.PubId = PubId;
            model.Price = Price;
            model.SellPrice = SellPrice;
            model.PubDate = PubDate;
            model.Ifhot = Ifhot;
            model.RecDate = RecDate;
            model.Jhzk = Jhzk;
            model.Pxzk = Pxzk;
            model.Lszk = Lszk;
            model.Author = Author;
            model.ZiShu = ZiShu;
            model.Zhzhen = Zhzhen;
            model.KaiBen = KaiBen;
            model.KcMax = KcMax;
            model.KcMin = KcMin;
            model.YinCi = YinCi;
            model.IsbnKind = IsbnKind;
            model.WeiZhi = WeiZhi;
            model.YinZhang = YinZhang;
            model.Note = Note;
            model.UpdateTime = UpdateTime;
            if (Id == 0) {
                model.CreateTime = CreateTime;
                model.Add();
            }
            else {
                model.Update();
            }
            this.Bind_DataList();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName).ToLower();
            int id = InputHelper.SetNumber(e.CommandArgument);
            switch (eName) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_DataDelete(id);
                    break;
            }
        }
        #endregion Tbl_B_Book结束




    }
}